package com.android.tnovoselec.travelassistant.db;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.android.tnovoselec.travelassistant.Config;
import com.android.tnovoselec.travelassistant.dto.BankomatDto;
import com.android.tnovoselec.travelassistant.dto.BenzinskaDto;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.nth.android.favorites.db.FavoritesDatabaseHelper;

public class TravelAssistantDatabaseHelper extends FavoritesDatabaseHelper {

    private static String DATABASE_NAME = "travelAssistant.db";
    private static int DATABASE_VERSION = 2;

    private Dao<BankomatDto, String> bankomatDao = null;
    private Dao<BenzinskaDto, String> benzinskaDao = null;
    private Context mContext;

    public TravelAssistantDatabaseHelper(Context context) {
	super(context, DATABASE_NAME, null, DATABASE_VERSION);
	this.mContext = context;
    }

    @Override
    public String getDatabaseName() {
	return DATABASE_NAME;
    }

    @Override
    public int getDatabaseVersion() {
	return DATABASE_VERSION;
    }

    @Override
    public List<String> getCreateQueries() {
	List<String> queries = new ArrayList<String>();
	queries.add(BankomatDto.getCreateQuery());
	queries.add(BenzinskaDto.getCreateQuery());
	return queries;
    }

    @Override
    public List<Class> getDtoClasses() {
	List<Class> classes = new ArrayList<Class>();
	classes.add(BankomatDto.class);
	return classes;
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource1) {
	super.onCreate(database, connectionSource1);
	BufferedReader reader = null;
	String line = null;
	try {
	    InputStream is = mContext.getAssets().open(Config.DATA_SCRIPT);
	    reader = new BufferedReader(new InputStreamReader(is));

	    // String line = null;
	    while ((line = reader.readLine()) != null) {
		database.execSQL(line);
		Log.i("Inserting", line);
	    }
	} catch (Exception e) {
	    Log.e(TravelAssistantDatabaseHelper.class.getSimpleName(), e.getMessage());
	} finally {
	    if (reader != null) {
		try {
		    reader.close();
		} catch (IOException e) {
		    // TODO Auto-generated catch block
		    Log.e(TravelAssistantDatabaseHelper.class.getSimpleName(), e.getMessage());
		}
	    }
	}
    }

    public Dao<BankomatDto, String> getBankomatDao() throws SQLException {
	if (bankomatDao == null) {
	    bankomatDao = super.getDao(BankomatDto.class);
	}

	return bankomatDao;
    }

    public Dao<BenzinskaDto, String> getBenzinskaDao() throws SQLException {
	if (benzinskaDao == null) {
	    benzinskaDao = super.getDao(BenzinskaDto.class);
	}

	return benzinskaDao;
    }

}
